package edu.nf.project.services.employeeAndCustomerInformation.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.convention.ShippingAddress;
import edu.nf.project.mappers.employeeAndCustomerInformation.convention.ShippingAddressMapper;
import edu.nf.project.services.employeeAndCustomerInformation.DefaultAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author CSY
 * @date 2025 - 06 - 10
 */
@Service
public class DefaultAddressServiceImpl implements DefaultAddressService {
    @Autowired
    private ShippingAddressMapper shippingAddressMapper;

    @Override
    public void isDefaultExit(ShippingAddress address) {
        // 使用 MyBatis-Plus 的 UpdateWrapper 批量更新
        // 构建 WHERE 子句条件：
        // 1. eq("u_id", address.getUId())：用户 ID 相等
        // 2. eq("is_default", true)：当前是默认地址
        // 3. ne("address_id", address.getAddressId())：排除当前要更新的地址
        UpdateWrapper<ShippingAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("u_id", address.getUId())
                .eq("is_default", true)
                .ne("address_id", address.getAddressId());

        ShippingAddress updateEntity = new ShippingAddress();
        updateEntity.setIsDefault(false);

        shippingAddressMapper.update(updateEntity, updateWrapper);
    }
}
